package com.cc2019.buke.user.Activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cc2019.buke.MainActivity;
import com.cc2019.buke.R;
import com.cc2019.buke.user.frag.UserFragment;
import com.cc2019.buke.user.tools.Constants;
import com.cc2019.buke.user.tools.DataBaseHelper;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    //Button button;

    SQLiteDatabase sqLiteDatabase;
    DataBaseHelper helper;

    private EditText username;
    private EditText password;
    private Button resign;
    private Button to_user;
    private Button forgetpassword;
    private Button backtoU;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_add);
        helper=new DataBaseHelper(this,"user.db",null,2);
        username=(EditText) findViewById(R.id.username);
        password=(EditText)findViewById(R.id.password);
        resign=(Button) findViewById(R.id.register_login);
        to_user=(Button) findViewById(R.id.to_user);
        backtoU=(Button) findViewById(R.id.back_to_userfragment);
        forgetpassword=(Button) findViewById(R.id.forget_password_login);

        sqLiteDatabase=helper.getWritableDatabase();

        //设置监听按钮
        resign.setOnClickListener(this);
        to_user.setOnClickListener(this);
        forgetpassword.setOnClickListener(this);
        backtoU.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        //通过id分配按钮事件
        switch (v.getId()) {
            //注册按钮
            case R.id.register_login:
                Intent intent=new Intent(this,RegisterActivity.class);
                startActivity(intent);
                break;

            //登录按钮
            case R.id.to_user:
                String user_str=username.getText().toString();
                String psw_str=password.getText().toString();

                //账号密码为空时
                if(user_str.equals("")){
                    Toast.makeText(this,"姓名不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    Cursor cursor=sqLiteDatabase.query(Constants.TABLE_NAME,new String[]{"password"}, "name=?", new String[]{user_str}, null, null, null);
                    //游标遍历，寻找name的password值
                    if(cursor.moveToNext()){
                        String psw_query=cursor.getString(cursor.getColumnIndex("password"));
                        if(psw_str.equals(psw_query)){
                            Toast.makeText(this,"登录成功！",Toast.LENGTH_SHORT).show();
                            Intent intent1=new Intent(this, MainActivity.class);
                            intent1.putExtra("username",user_str);
                            startActivity(intent1);
                            break;
                        }else  if(!TextUtils.isEmpty(user_str)&& TextUtils.isEmpty(psw_str)){
                            Toast.makeText(LoginActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
                        }else {
                            Intent intent2=new Intent(this, FaultActivity.class);
                            startActivity(intent2);
                        }
                    }
                    else {
                        Toast.makeText(this,"账号不存在，请注册！",Toast.LENGTH_SHORT).show();
                    }

                }
                break;

            case R.id.forget_password_login:
                Intent intent2=new Intent(this,ForgetPasswordActivity.class);
                startActivity(intent2);
                break;
            case R.id.back_to_userfragment:
                Intent intent3=new Intent(this,MainActivity.class);
                startActivity(intent3);
                break;

        }
    }
}
